# Maintainer: psykose <alice@ayaya.dev>
pkgname=code-oss
pkgver=1.69.1
pkgrel=0
pkgdesc="Visual Studio Code (OSS, with VSX)"
url="https://github.com/microsoft/vscode"
arch="aarch64 x86_64" # electron
license="MIT"
depends="electron ripgrep"
makedepends="
	libsecret-dev
	libxkbfile-dev
	nodejs
	npm
	python3
	yarn
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/microsoft/vscode/archive/refs/tags/$pkgver.tar.gz
	electron-version.patch
	enable-extensions.patch
	launcher.patch
	no-git.patch
	"
builddir="$srcdir/vscode-$pkgver"
options="!check" # no tests

export ELECTRON_SKIP_BINARY_DOWNLOAD=1
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1

prepare() {
	default_prepare
	# block husky
	git init .

	yarn config set python python3
	yarn install --frozen-lockfile
}

build() {
	node --max_old_space_size=4096 ./node_modules/.bin/gulp vscode-linux-x64-min
}

package() {
	mkdir -p "$pkgdir"/usr/share/applications
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code.desktop > "$pkgdir"/usr/share/applications/code-oss.desktop
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code-url-handler.desktop > "$pkgdir"/usr/share/applications/code-oss-url-handler.desktop

	install -Dm644 resources/linux/code.png "$pkgdir"/usr/share/pixmaps/com.visualstudio.code.oss.png

	mkdir -p "$pkgdir"/usr/share/appdata
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@LICENSE@@|MIT|g" \
		resources/linux/code.appdata.xml > "$pkgdir"/usr/share/appdata/code-oss.appdata.xml

	mkdir -p "$pkgdir"/usr/share/mime/packages
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		resources/linux/code-workspace.xml > "$pkgdir"/usr/share/mime/packages/code-oss-workspace.xml

	mkdir -p "$pkgdir"/usr/share/bash-completion/completions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/bash/code > "$pkgdir"/usr/share/bash-completion/completions/code-oss

	mkdir -p "$pkgdir"/usr/share/zsh/site-functions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/zsh/_code > "$pkgdir"/usr/share/zsh/site-functions/_code-oss

	mkdir -p "$pkgdir"/usr/lib/code-oss/resources
	cp -a ../VSCode-linux-x64/resources/app "$pkgdir"/usr/lib/code-oss/resources/

	# link to system rg
	ln -sfv /usr/bin/rg \
		"$pkgdir"/usr/lib/code-oss/resources/app/node_modules.asar.unpacked/@vscode/ripgrep/bin/rg

	mkdir -p "$pkgdir"/usr/bin
	sed \
		-e "s|VSCODE_PATH=/usr/share/vscode|VSCODE_PATH=/usr/lib/code-oss|g" \
		-e "s|ELECTRON=.*|ELECTRON=/usr/lib/electron/electron|g" \
		-e 's|"$CLI"|"$CLI" --app="${VSCODE_PATH}/resources/app"|g' \
		../VSCode-linux-x64/bin/code-oss > "$pkgdir"/usr/bin/code-oss
	chmod +x "$pkgdir"/usr/bin/code-oss
}

sha512sums="
d1965c4cb75127228461c0382e044efa7e9e090f8bd1f6b3afb8867cc931065b459dcec65c7a6963e3de06abae576fcf842de80acbf665ec385b558fdd90ea8c  code-oss-1.69.1.tar.gz
e7c8fc4fcca4e4b38c4dd87989da38fb114e62df3de2444a638d76a5db87371af1d3c933edcad757c6df7b7dfc5bf2e08ea5660af704df9a08b9c864b4482b81  electron-version.patch
c8c16511a8cb45f389bd8847d801a6c92d2be443a1a6de383acac575bbcdfd9e97af894d388803627a0cf0552ab46e85234f206db9bd720d8f2ce8443db92a66  enable-extensions.patch
445fde371eab1df87a74024cb624bf8f89cf7dd2c50c912de2ab9f6fa05546b2fbb1595dd316e7febca959c5c671c554faf8fef499de520811ea91b48bd1ee15  launcher.patch
b91c35bed74da482d63137f43529ec38dc10a293f52a4d1d1c0d6955d84d5f19e61512147302eaa0901152632bf148bf7b99775315e8e0c00bcf2a2e9e585a0a  no-git.patch
"
